package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        //test01();
        //test02();
        //test03();
        //test04();
        test05();
    }

    private static void test05() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://baidu.com");
        webDriver.manage().window().maximize();
        //webDriver.navigate().to();
        //webDriver.navigate().forward();
        //webDriver.navigate().back();
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=1000");

    }

    private static void test04() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        WebElement search_input = webDriver.findElement(By.cssSelector("#kw"));
        search_input.sendKeys("哔哩哔哩");
        WebElement button = webDriver.findElement(By.cssSelector("#su"));
        button.click();
        sleep(1000);
        //webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        search_input.clear();
        search_input.sendKeys("b站");
        button.click();
    }

    private static void test03() {
        // 创建驱动
        WebDriver webDriver = new ChromeDriver();
        // 打开百度首页
        webDriver.get("https://www.baidu.com");
        // 找到新闻按钮
        WebElement news_button = webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)"));
        // 点击新闻
        news_button.click();
        // 退出浏览器
        //webDriver.quit();
    }

    private static void test02() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://baidu.com");
        WebElement search_input = webDriver.findElement(By.cssSelector("#kw"));
        search_input.sendKeys("哔哩哔哩");

        WebElement button = webDriver.findElement(By.cssSelector("#su"));
        button.click();

        sleep(1000);

        WebElement address = webDriver.findElement(By.cssSelector("#\\31  > div > div:nth-child(1) > h3 > a:nth-child(1)"));
        address.click();
    }

    private static void test01() throws InterruptedException {
        //1.打开百度首页
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://baidu.com");

        WebElement search_input = webDriver.findElement(By.cssSelector("#kw"));
        search_input.sendKeys("哔哩哔哩");

        WebElement button = webDriver.findElement(By.cssSelector("#su"));
        button.click();

        sleep(1000);

        List<WebElement> results = webDriver.findElements(By.cssSelector("#\\31  > div > div:nth-child(1) > h3 > a:nth-child(1) > em"));
        sleep(1000);

        if(results.size() != 0){
            System.out.println(results.size());
            System.out.println("测试通过~");
        }else{
            System.out.println("测试不通过");
        }
    }
}